<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/12/12
 * Time: 13:51
 */
namespace App\Services\BackendApi;

use App\Repositories\RepaymentBillRepository;
use App\Repositories\RepaymentPlanConsumeRepository;
use App\Repositories\RepaymentPlanRepaymentRepository;
use App\Repositories\RepaymentPlanRepository;
use App\Repositories\UserLevelRelationsRepository;
use App\Repositories\UserRepository;

class RepaymentService extends CommonService
{
    protected $userRepository;
    protected $userLevelRelationsRepository;
    protected $repaymentBillRepository;
    protected $repaymentPlanRepository;
    protected $repaymentPlanRepaymentRepository;
    protected $repaymentPlanConsumeRepository;

    public function __construct(UserRepository $userRepository,
                                UserLevelRelationsRepository $userLevelRelationsRepository,
                                RepaymentBillRepository $repaymentBillRepository,
                                RepaymentPlanRepository $repaymentPlanRepository,
                                RepaymentPlanRepaymentRepository $repaymentPlanRepaymentRepository,
                                RepaymentPlanConsumeRepository $repaymentPlanConsumeRepository)
    {
        parent::__construct();
        $this->userRepository = $userRepository;
        $this->repaymentBillRepository = $repaymentBillRepository;
        $this->repaymentPlanRepository = $repaymentPlanRepository;
        $this->repaymentPlanRepaymentRepository = $repaymentPlanRepaymentRepository;
        $this->repaymentPlanConsumeRepository = $repaymentPlanConsumeRepository;
    }

    public function getList($request)
    {
        if ($request['bill_date_start'] > $request['bill_date_end'] ||
            $request['repayment_date_start'] > $request['repayment_date_end'] ||
            $request['finish_plan_time_start'] > $request['finish_plan_time_end']) {
            return $this->responseJson(1010000);//开始时间大于结束时间
        }
        $data = $this->repaymentPlanRepository->getRepaymentList($request);
        return $this->responseJson(0, $data);
    }

    /**
     * 还款计划详情
     * @param $planId
     */
    public function repaymentPlanDetail($planId, $userId)
    {
        $getPlan = $this->repaymentPlanRepository->getOnePlan($planId, $userId);
        if (empty($getPlan)) {
            return $this->responseJson(1020000);//还款计划不存在
        }
        $getPlan = $getPlan->toArray();
        $bill = $this->repaymentBillRepository->byId($getPlan['repayment_bill_id']);
        $getPlan['credit_card_id'] = $bill->credit_card_id;
        $data = [];
        $getPlanRepayment = $this->repaymentPlanRepaymentRepository->planRepaymentAll($planId);
        if (!empty($getPlanRepayment)) {
            $getPlanRepayment = $getPlanRepayment->toArray();
            $getPlanRepayment = unsetColumn($getPlanRepayment, ['sub_merchant_no', 'bank_no']);
            foreach ($getPlanRepayment as $key=>$value) {
                $data[$key] = $value;
                $getPlanConsume = $this->repaymentPlanConsumeRepository->planRepaymentConsumeAll($value['id'])->toArray();
                $getPlanConsume = $this->consumeAddressUnserialize($getPlanConsume);
                $getPlanConsume = unsetColumn($getPlanConsume, ['sub_merchant_no', 'bank_no']);
                $data[$key]['consume'] = $getPlanConsume;
            }
            $getPlan['detail'] = $data;
        }
        return $this->responseJson(0, $getPlan);
    }

    protected function consumeAddressUnserialize($data)
    {
        foreach ($data as $key=>$value) {
            $data[$key]['consume_address'] = $this->consumeAddressStringFormat($value['consume_address']);
        }
        return $data;
    }

    protected function consumeAddressStringFormat($consumeAddress)
    {
        $data = unserialize($consumeAddress);
        if ($data && !empty($data)) {
            $province = isset($data['province']) ? $data['province'] : '';
            $city = isset($data['city']) ? $data['city'] : '';
            $area = isset($data['area']) ? $data['area'] : '';
            return $province . $city . $area;
        }
        return '';
    }

    public function jobEdit()
    {
        $consumeLockTimeRedisKey = $this->consumeLockTimeRedisKey();
        $consumeTime = $this->redis()->get($consumeLockTimeRedisKey);
        $repaymentLockTimeRedisKey = $this->repaymentLockTimeRedisKey();
        $repaymentTime = $this->redis()->get($repaymentLockTimeRedisKey);
        return $this->responseJson(0, [
            'consume_time' => $consumeTime,
            'repayment_time' => $repaymentTime,
        ]);

    }

    public function jobUpdate($type, $value)
    {
        if (!in_array($type, ['consume', 'repayment'])) {
            return $this->responseJson(1040000);//不存在该任务类型
        }
        $time = strtotime($value);
        if (!$time) {
            $time = 0;
        }
        if ($type == 'consume') {
            $consumeLockTimeRedisKey = $this->consumeLockTimeRedisKey();
            $value = $this->redis()->set($consumeLockTimeRedisKey, $time);
        } else {
            $repaymentLockTimeRedisKey = $this->repaymentLockTimeRedisKey();
            $value = $this->redis()->set($repaymentLockTimeRedisKey, $time);
        }
        return $this->responseJson(0, [
            'type' => $type,
            'value' => $value,
        ]);
    }


}